package com.mos.action;

import com.mos.base.Globl;
import com.mos.base.action.BaseAction;
import com.mos.entity.Menu;
import com.mos.service.MenuService;
import com.mos.util.RandomUtil;
import com.mos.util.TreeUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * Package Name: com.mos.action
 * User: grq
 * Date: 2016/3/28
 * Time: 14:31
 * Description:some des here!
 */
@Controller
@RequestMapping("/menu")
public class MenuAction extends BaseAction {

    private static final String PREFIX = "/menu/";

    @Autowired
    private MenuService menuService;

    @RequestMapping(value = "/addPage", method = RequestMethod.GET)
    public String addPage() {
        System.out.println("add");
        return PREFIX + "add";
    }


    @ResponseBody
    @RequestMapping(value = "/save")
    public String add(@RequestParam(value = "systemIconName", required = false) String systemIconName,
                      @RequestParam(value = "systemIconPath", required = false) String systemIconPath ,
                      @RequestParam(value = "icon_source", required = true) String type ,
                      Menu menu, HttpSession session) throws Exception {

        String userId = super.getNowUserId(session);
        menu.setCreaterId(userId);
        menu.setUpdaterId(userId);
        menu.setCreateTime(new Date());
        menu.setUpdateTime(new Date());

        menu.setId(RandomUtil.createUuid());
        menu.setDel(Globl.NEGATIVE);
        menu.setIsShow(StringUtils.isBlank(menu.getIsShow()) ? Globl.POSITIVE : menu.getIsShow());

        Menu parent = menuService.findById(menu.getPid());
        menu.setPid(parent == null ? "" : parent.getId());
        menu.setTraceIds(parent == null ? menu.getId() + Globl.DEFAULT_SEPARATOR : parent.getTraceIds() + menu.getId() + Globl.DEFAULT_SEPARATOR);
        menuService.save(menu,systemIconName,systemIconPath,type);
        return "success";
    }

    @RequestMapping(value = "listPage", method = RequestMethod.GET)
    public String listPage() {
        System.out.println("come in list page.");
        return PREFIX + "list";
    }

    @ResponseBody
    @RequestMapping(value = "/load")
    public List load() {
        List<Map<String, Object>> menus = menuService.queryList(null);

        List<Object> listZTree = new ArrayList<Object>();
        String str = "";
        for (int i = 0; i < menus.size(); i++) {
            Map<String, Object>  menu = menus.get(i);//分类信息
            if(menu.get("pid") == null) continue;
            str = "{id:'" +menu.get("id") + "', pId:'"+menu.get("pid")+"', name:'"+menu.get("name")+"',path:'"+menu.get("path")+"' }";//封装ztree需要格式的字符串
            listZTree.add(str);
        }
        return listZTree;
       /* List<Map<String, Object>> easyUIList = TreeUtil.tree(list, "id", "pid", "name");
        return easyUIList;*/
    }

    @ResponseBody
    @RequestMapping(value = "/loadLeftMenu")
    public List load(@RequestParam(defaultValue = "0")String pid) {
        List<Map<String, Object>> list = menuService.queryListByPid(pid);
        return list;
       /* List<Map<String, Object>> easyUIList = TreeUtil.tree(list, "id", "pid", "name");
        return easyUIList;*/
    }

}
